package com.example.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.domain.MFile;

import java.io.*;

public class SerializeUtils {

    public static <T> byte[] serizlize(T t) throws Exception{
        if(!(t instanceof Serializable)){
            throw new Exception("未实现 Serializable ");
        }
        return serizlizeByObject(t);
    }

    public static <T> T deserialize(byte[] bytes,Class<T> cls) throws Exception{
        if(!(cls instanceof Serializable)){
            throw new Exception("未实现 Serializable ");
        }
        return (T) deserializeByObject(bytes);
    }

    /*
     * 序列化
     * */
    public static byte[] serizlizeByObject(Object object){
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(baos != null){
                    baos.close();
                }
                if (oos != null) {
                    oos.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    /*
     * 反序列化
     * */
    public static Object deserializeByObject(byte[] bytes){
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try{
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            return ois.readObject();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {

            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }


    public static <T> void serializeObject(T t) throws IOException{
        String className=t.getClass().getName();
        String serializePath=PathUils.getSerializePath(className);
        ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
                                new File(serializePath)));
                 oo.writeObject(t);
        oo.close();
    }

    public static <T> T deSerializeObject(String path) throws Exception{
        ObjectInputStream ois=null;
        try {
            ois= new ObjectInputStream(new FileInputStream(
                    new File(path)));
            return (T)ois.readObject();
        }finally {
            if(ois!=null){
                ois.close();
            }
        }
    }


    public static void main(String[] args) throws Exception {
//        MFile mFile=new MFile();
//        mFile.setCreatetime(new Date());
//        mFile.setFilename("文件名");
//        mFile.setFid(1);

//        byte[] bytes=SerializeUtils.serizlizeByObject(mFile);
//        String indexname=PathUils.getIndexPath("123");
//        writeBytesToFile(bytes,indexname);
        String indexname=PathUils.getIndexPath("123");
        byte[] bytes=IOUtils.getContent(indexname);
        MFile mFile=SerializeUtils.deserialize(bytes,MFile.class);
        System.out.println(JSONObject.toJSONString(mFile));

    }


}
